#!/usr/bin/env python3

import serial


CR = b'\x0D'
STX = b'\x02'
ETX = b'\x03'


def run(port):
    with serial.Serial(port, 19200, timeout=1) as ser:
        while True:
            try:
                cmd = input('> ')
            except EOFError:
                print()  # Add final newline
                break
            ser.write(cmd.encode() + CR)
            rsp = b''
            while True:
                byte = ser.read()
                if byte == STX:
                    continue
                if byte == ETX:
                    break
                rsp += byte
            print(rsp.decode())


if __name__ == '__main__':
    import sys
    run(sys.argv[1])
